import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-12-07
 * 8:55
 */
 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public static void main(String[] args) {
        ListNode cur1=new ListNode(1);
        ListNode cur2=cur1;

        ListNode cur3=FindFirstCommonNode(cur1,cur2);
        System.out.println(cur3.val);
    }
    public static ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        Map<ListNode, Integer> map1 = new LinkedHashMap<>();

        while (pHead1 != null) {

            if (!map1.containsKey(pHead1)) {
                map1.put(pHead1, 1);

            } else if (map1.containsKey(pHead1)) {
                map1.put(pHead1, map1.get(pHead1) + 1);

            }
            pHead1 = pHead1.next;
        }
        while (pHead2 != null) {
            if (!map1.containsKey(pHead2)) {
                map1.put(pHead2, 1);
            } else if (map1.containsKey(pHead2)) {
                map1.put(pHead2, map1.get(pHead2) + 1);
            }

            pHead2 = pHead2.next;
        }
        for (Map.Entry<ListNode, Integer> entry : map1.entrySet()) {
            if (entry.getValue() == 2) {
                return entry.getKey();
            }
        }
        return null;
    }
}